home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_xmlrpc.py < prev    next >
Text File  |  2005-10-18  |  3KB  |  97 lines

  1. import sys
  2. import unittest
  3. import xmlrpclib
  4. from test import test_support
  5.  
  6. try:
  7.     unicode
  8. except NameError:
  9.     have_unicode = False
  10. else:
  11.     have_unicode = True
  12.  
  13. alist = [{'astring': 'foo@bar.baz.spam',
  14.           'afloat': 7283.43,
  15.           'anint': 2**20,
  16.           'ashortlong': 2L,
  17.           'anotherlist': ['.zyx.41'],
  18.           'abase64': xmlrpclib.Binary("my dog has fleas"),
  19.           'boolean': xmlrpclib.False,
  20.           'unicode': u'\u4000\u6000\u8000',
  21.           u'ukey\u4000': 'regular value',
  22.           }]
  23.  
  24. class XMLRPCTestCase(unittest.TestCase):
  25.  
  26.     def test_dump_load(self):
  27.         self.assertEquals(alist,
  28.                           xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0])
  29.  
  30.     def test_dump_big_long(self):
  31.         self.assertRaises(OverflowError, xmlrpclib.dumps, (2L**99,))
  32.  
  33.     def test_dump_bad_dict(self):
  34.         self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},))
  35.  
  36.     def test_dump_big_int(self):
  37.         if sys.maxint > 2L**31-1:
  38.             self.assertRaises(OverflowError, xmlrpclib.dumps,
  39.                               (int(2L**34),))
  40.  
  41.     def test_dump_none(self):
  42.         value = alist + [None]
  43.         arg1 = (alist + [None],)
  44.         strg = xmlrpclib.dumps(arg1, allow_none=True)
  45.         self.assertEquals(value,
  46.                           xmlrpclib.loads(strg)[0][0])
  47.         self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,))
  48.  
  49.     def test_default_encoding_issues(self):
  50.         # SF bug #1115989: wrong decoding in '_stringify'
  51.         utf8 = """<?xml version='1.0' encoding='iso-8859-1'?>
  52.                   <params>
  53.                     <param><value>
  54.                       <string>abc \x95</string>
  55.                       </value></param>
  56.                     <param><value>
  57.                       <struct>
  58.                         <member>
  59.                           <name>def \x96</name>
  60.                           <value><string>ghi \x97</string></value>
  61.                           </member>
  62.                         </struct>
  63.                       </value></param>
  64.                   </params>
  65.                   """
  66.  
  67.         # sys.setdefaultencoding() normally doesn't exist after site.py is
  68.         # loaded.  reload(sys) is the way to get it back.
  69.         old_encoding = sys.getdefaultencoding()
  70.         setdefaultencoding_existed = hasattr(sys, "setdefaultencoding")
  71.         reload(sys) # ugh!
  72.         sys.setdefaultencoding("iso-8859-1")
  73.         try:
  74.             (s, d), m = xmlrpclib.loads(utf8)
  75.         finally:
  76.             sys.setdefaultencoding(old_encoding)
  77.             if not setdefaultencoding_existed:
  78.                 del sys.setdefaultencoding
  79.  
  80.         items = d.items()
  81.         if have_unicode:
  82.             self.assertEquals(s, u"abc \x95")
  83.             self.assert_(isinstance(s, unicode))
  84.             self.assertEquals(items, [(u"def \x96", u"ghi \x97")])
  85.             self.assert_(isinstance(items[0][0], unicode))
  86.             self.assert_(isinstance(items[0][1], unicode))
  87.         else:
  88.             self.assertEquals(s, "abc \xc2\x95")
  89.             self.assertEquals(items, [("def \xc2\x96", "ghi \xc2\x97")])
  90.  
  91. def test_main():
  92.     test_support.run_unittest(XMLRPCTestCase)
  93.  
  94.  
  95. if __name__ == "__main__":
  96.     test_main()
  97.